import { AuthGuard } from "@nestjs/passport";
import { ExecutionContext, Injectable } from "@nestjs/common";
import { AuthException } from "@exceptions";
import { Reflector } from "@nestjs/core";
import { JwtService } from "@nestjs/jwt";
import { AuthService } from "@/modules/auth/auth.service";


@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {
    constructor(
        private authService: AuthService,
        private reflector: Reflector) {
        super();
    }
    canActivate(context: ExecutionContext) {
        const noAuth = this.reflector.get<string[]>('no-auth', context.getHandler())
        if (noAuth) return true
        return super.canActivate(context);
    }

    handleRequest(err, user, info, ctx): any {
        const token: string = ctx.getRequest().headers['access_token']
        if (err || !user) {
            throw new AuthException()
        }
        return user;
    }
}
